package com.wao.cms.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.wao.cms.service.PropertyValueService;
import com.wao.cms.service.dto.PropertyValueDTO;
import com.wao.cms.web.rest.util.HeaderUtil;
import com.wao.cms.web.rest.util.PaginationUtil;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing PropertyValue.
 */
@RestController
@RequestMapping("/api")
public class PropertyValueResource {

    private final Logger log = LoggerFactory.getLogger(PropertyValueResource.class);

    private static final String ENTITY_NAME = "propertyValue";

    private final PropertyValueService propertyValueService;

    public PropertyValueResource(PropertyValueService propertyValueService) {
        this.propertyValueService = propertyValueService;
    }

    /**
     * POST  /property-values : Create a new propertyValue.
     *
     * @param propertyValueDTO the propertyValueDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new propertyValueDTO, or with status 400 (Bad Request) if the propertyValue has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/property-values")
    @Timed
    public ResponseEntity<PropertyValueDTO> createPropertyValue(@Valid @RequestBody PropertyValueDTO propertyValueDTO) throws URISyntaxException {
        log.debug("REST request to save PropertyValue : {}", propertyValueDTO);
        if (propertyValueDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new propertyValue cannot already have an ID")).body(null);
        }
        PropertyValueDTO result = propertyValueService.save(propertyValueDTO);
        return ResponseEntity.created(new URI("/api/property-values/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /property-values : Updates an existing propertyValue.
     *
     * @param propertyValueDTO the propertyValueDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated propertyValueDTO,
     * or with status 400 (Bad Request) if the propertyValueDTO is not valid,
     * or with status 500 (Internal Server Error) if the propertyValueDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/property-values")
    @Timed
    public ResponseEntity<PropertyValueDTO> updatePropertyValue(@Valid @RequestBody PropertyValueDTO propertyValueDTO) throws URISyntaxException {
        log.debug("REST request to update PropertyValue : {}", propertyValueDTO);
        if (propertyValueDTO.getId() == null) {
            return createPropertyValue(propertyValueDTO);
        }
        PropertyValueDTO result = propertyValueService.save(propertyValueDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, propertyValueDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /property-values : get all the propertyValues.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of propertyValues in body
     */
    @GetMapping("/property-values")
    @Timed
    public ResponseEntity<List<PropertyValueDTO>> getAllPropertyValues(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of PropertyValues");
        Page<PropertyValueDTO> page = propertyValueService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/property-values");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /property-values/:id : get the "id" propertyValue.
     *
     * @param id the id of the propertyValueDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the propertyValueDTO, or with status 404 (Not Found)
     */
    @GetMapping("/property-values/{id}")
    @Timed
    public ResponseEntity<PropertyValueDTO> getPropertyValue(@PathVariable Long id) {
        log.debug("REST request to get PropertyValue : {}", id);
        PropertyValueDTO propertyValueDTO = propertyValueService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(propertyValueDTO));
    }

    /**
     * DELETE  /property-values/:id : delete the "id" propertyValue.
     *
     * @param id the id of the propertyValueDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/property-values/{id}")
    @Timed
    public ResponseEntity<Void> deletePropertyValue(@PathVariable Long id) {
        log.debug("REST request to delete PropertyValue : {}", id);
        propertyValueService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * GET  /pages/{id}/properties : get propertyValues of the page "id".
     *
     * @param id the id of the propertyValueDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the propertyValueDTO, or with status 404 (Not Found)
     */
    @GetMapping("/pages/{id}/properties")
    @Timed
    public ResponseEntity<List<PropertyValueDTO>> getPropertyValuesByPage(@PathVariable Long id) {
        log.debug("REST request to get PropertyValue : {}", id);
        List<PropertyValueDTO> propertyValueDTOs = propertyValueService.findByPageId(id);
        return ResponseEntity.ok(propertyValueDTOs);
    }
}
